<?php

namespace app\admin\model\ykjp\purchase;

use think\Model;
use traits\model\SoftDelete;

class Storage extends Model {

    use SoftDelete;

    // 表名
    protected $name = 'ykjp_storage';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
        'delivery_time_text',
        'the_way_text',
        'status_text'
    ];

    public function getTheWayList() {
        return ['2' => __('The_way 2'), '1' => __('The_way 1'), '0' => __('The_way 0')];
    }

    public function getStatusList() {
        return ['2' => __('Status 2'), '1' => __('Status 1'), '0' => __('Status 0'), '3' => __('Status 3')];
    }

    public function getDeliveryTimeTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['delivery_time']) ? $data['delivery_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getTheWayTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['the_way']) ? $data['the_way'] : '');
        $list = $this->getTheWayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setDeliveryTimeAttr($value) {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function supplier() {
        return $this->belongsTo('app\admin\model\ykjp\information\basisinfo\Supplier', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function object_array($array) {
        if (is_object($array)) {
            $array = (array) $array;
        } if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }

}
